var mine;
$(function () {
    mine = new Vue({
        el:'#mineController',
        data:{
            nav:_mineNav,
            change:false,
            userInfo:{
                headImg:'',
                nickName:'',
                sex:0,
                birthday:'',
                job:'',
                sign:'',
                area:'',
                userState:0
            },
            pwd:{
                old:'',
                newPwd:'',
                repeat:''
            }
        },
        methods:{
            loadData:function () {
                var that = this;
                sy.postApi('/api/userInfo/userInfo',{},function (data) {
                    that.userInfo.headImg = data.headImg;
                    that.userInfo.nickName = data.nickName;
                    that.userInfo.sex = data.sex;
                    that.userInfo.birthday = data.birthday;
                    that.userInfo.job = data.job;
                    that.userInfo.sign = data.sign;
                    that.userInfo.area = data.area;
                    that.userInfo.userState = data.userState;
                })
            },
            changePwd:function () {
                var that = this;
                var pwd = that.pwd;
                if (!pwd.old){
                    sy.msg('请输入原密码')
                    return;
                }
                if (!pwd.newPwd){
                    sy.msg('请输入新密码')
                    return;
                }
                if (!pwd.repeat){
                    sy.msg('请确认新密码')
                    return;
                }
                if (!(pwd.newPwd == pwd.repeat)){
                    sy.msg('两次密码不相同');
                    return;
                }
                sy.post('/web/home/changePwd',{
                    old:hex_md5(pwd.old),
                    newPwd:hex_md5(pwd.newPwd)
                },function (data) {
                    if (data.code == 0){
                        that.change = false;
                        sy.msg('个人信息修改成功');
                        location.reload();
                    }else{
                        sy.msg(data.msg);
                    }
                })
            },
            save:function () {
                var that = this;
                sy.post('/web/home/editInfo',that.userInfo,function (data) {
                    if (data.code == 0){
                        sy.msg('个人信息修改成功');
                        top.loadData();
                        location.reload();
                    }
                })
            },
            logout:function () {
                location.href = sy.path+'/web/logout?back_url='+encodeURI(location.href);
            },
            changePwdClick:function (value) {
                this.change = value;
            },
            uploadImg:function () {
                if (this.userInfo.userState == 0){
                    $("#img").trigger("click");
                }
            },
            updateImgs:function (img) {
                this.userInfo.headImg = img;
                sy.closeLoad();
            }
        },
        created:function () {
            this.loadData();
        },
        mounted:function () {
            $("#img").bind("change",function(){
                if($(this).val() == '')return;
                sy.openLoad();
                $("#form1").submit();
            });
            var that = this;
            //选择年月日
            laydate.render({
                elem: '#select_date',
                trigger: 'click',
                done: function(value){
                    that.userInfo.birthday = value;
                }
            });
            // 选择省市区
            $("#city").click(function (e) {
                SelCity(this,e,function (area) {
                    that.userInfo.area = area;
                });
            });
        }
    });
})

/**
 * 图片上传回调
 * @param id
 */
function callback(id) {
    mine.updateImgs(id);
}